#include <stdio.h>
#include <string.h>

static int stepnum = 0;
static char state[64] = {0};
static cir_cnt = 5;

static void devide(int count);
static void combine(int count);

static print_state()
{
    int i;
    for (i = 0; i < cir_cnt; i ++)
    {
        printf("%d ", state[i]);
    }
    printf("\n");
}

static void do_up(int count)
{
    stepnum++;
    state[count - 1] = 1;
    print_state();
    // printf("step[%04d]: up %d\n", stepnum ++, count);
}

static void do_down(int count)
{
    stepnum++;
    state[count - 1] = 0;
    print_state();
    // printf("step[%04d]: down %d\n", stepnum ++, count);
}

static void combine(int count)
{
    if (count == 1)
    {
        do_up(count);
    }
    else if (count == 2)
    {
        do_up(count - 1);
        do_up(count);
    }
    else
    {
        combine(count - 1);
        devide(count - 2);
        do_up(count);
        combine(count - 2);
    }

}

static void devide(int count)
{
    // printf("devide %d\n", count);
    if (count == 1)
    {
        do_down(count);
    }
    else if (count == 2)
    {
        do_down(count);
        do_down(count - 1);
    }
    else
    {
        devide(count - 2);
        do_down(count);
        combine(count - 2);
        devide(count - 1);
    }
}
void usage()
{
    printf("nine_circle usage:\n");
    printf("nine_circle <num> <op>\n");
    printf("    num : the number of circle, 1,2,3, ...\n");
    printf("    op : up or down\n");
    printf("example:nine_circle 5 up\n");
}

int main(int argc, char **argv)
{
    if (argc < 3)
    {
        usage();
        return -1;
    }
    cir_cnt = atoi(argv[1]);
    if (strcmp(argv[2], "up") == 0)
    {
        memset(state, 0, sizeof(state));
        combine(cir_cnt);
    }
    else if (strcmp(argv[2], "down") == 0)
    {
        memset(state, 1, sizeof(state));
        devide(cir_cnt);
    }
    else
    {
        usage();
        return -1;
    }
    printf("circle=%d,stepnum=%d\n", cir_cnt, stepnum);
    return 0;
}
